package com.igeekhome.controller.api;

import com.igeekhome.pojo.BaseDict;
import com.igeekhome.service.IDiceService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 提供字典表的单表操作（封装）
 * @author Romeo
 */
@Component("baseDictControllerApi")
@Data
@Accessors(chain = true)

public class BaseDictControllerApi {

    @Autowired
    private IDiceService dictService;


    private String code;
    private String code_name;

    public List<BaseDict> list(){
        return dictService.list(code);
    }


    public TableResultBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        return dictService.list(page, limit, code);
    }

    public ResultBean add(BaseDict baseDict){
        // 传递状态码以及状态默认为启用
        baseDict.setDictTypeCode(code).setDictEnable("1").setDictTypeName(code_name);
        return dictService.add(baseDict);
    }

    public ResultBean edit(BaseDict baseDict){
        // 传递状态码
        baseDict.setDictTypeCode(code);
        return dictService.edit(baseDict);
    }
    public ResultBean delete(String idStr){
        if(idStr.contains(",")){
            idStr = idStr.substring(0,idStr.length()-1);
        }
        return dictService.delete(idStr);
    }
}
